use dataset_county, clear

*** make counterfactual county-level vote share ***

gen demvotescf = demvotes
gen repvotescf = repvotes

replace demvotescf = (l4.demvoteshare + l4.ddem)/100*(demvotes+repvotes) if flipdemrep == 1 
replace repvotescf = (repvotes + demvotes) - demvotescf if flipdemrep == 1 
replace repvotescf = (l4.demvoteshare + l4.ddem)/100*(demvotes+repvotes) if fliprepdem == 1
replace demvotescf = (repvotes + demvotes) - demvotescf if fliprepdem == 1 

replace demvotescf = l4.demvotes if (flipdemrep == 1 | fliprepdem == 1) 
replace repvotescf = l4.repvotes if (flipdemrep == 1 | fliprepdem == 1) 

*** aggregate up to state level ***

keep if year >= 1960 & !mi(stateabbr)
collapse ev (sum) demvotes repvotes demvotescf repvotescf, by(stateabbr year)

*** remake dem percent of vote ***

gen demvoteshare = (demvotes) / (demvotes + repvotes) * 100
gen demvotesharecf = (demvotescf) / (demvotescf + repvotescf) * 100

*** fix 1960 odd cases ***

replace demvoteshare = 50.03 if stateabbr=="HI" & year == 1960
replace demvotesharecf = 50.03 if stateabbr=="HI" & year == 1960
replace demvoteshare = 49 if stateabbr=="AK" & year == 1960
replace demvotesharecf = 49 if stateabbr=="AK" & year == 1960
replace demvoteshare = 51 if stateabbr=="MN" & year == 1960
replace demvotesharecf = 51 if stateabbr=="MN" & year == 1960
replace demvoteshare = 53 if stateabbr=="NY" & year == 1960
replace demvotesharecf = 53 if stateabbr=="NY" & year == 1960

*** fix 1964 odd cases ***

foreach x in demvoteshare demvotesharecf {
	replace `x' = 65.91 if stateabbr == "AK" & year == 1964
	replace `x' = 86 if stateabbr == "DC" & year == 1964
}

*** fix 2016 odd cases ***

replace demvoteshare = 42 if stateabbr=="AK" & year == 2016
replace demvotesharecf = 42 if stateabbr=="AK" & year == 2016

*** make dem and rep ev ***

gen demev = .
replace demev = ev if demvoteshare > 50 & !mi(demvoteshare)
replace demev = 0 if demvoteshare < 50 & !mi(demvoteshare)
gen demevcf = .
replace demevcf = ev if demvotesharecf > 50 & !mi(demvotesharecf)
replace demevcf = 0 if demvotesharecf < 50 & !mi(demvotesharecf)

*** fix 1960 odd cases ***

foreach x in demev demevcf  {
	replace `x' = `x' - 6 if statea=="AL" & year == 1960
	replace `x' = 0 if statea=="MS" & year == 1960
}

*** fix 1968 odd cases ***

foreach x in demev demevcf {
	replace `x' = 0 if statea=="AL" & year == 1968
	replace `x' = 3 if statea=="DC" & year == 1968
	replace `x' = 0 if statea=="LA" & year == 1968
	replace `x' = 0 if statea=="MS" & year == 1968
	replace `x' = 43 if statea=="NY" & year == 1968
}

*** fix 1972 odd cases ***

foreach x in demev demevcf {
	replace `x' = 0 if statea=="WI" & year == 1972
	replace `x' = 3 if statea=="DC" & year == 1972
}

*** fix 1976 odd cases ***

foreach x in demev demevcf {
	replace `x' = 41 if statea=="NY" & year == 1976
	replace `x' = 3 if statea=="DC" & year == 1976
}

*** fix 1980-4, 1992-6, 2004, 2012 odd cases ***

foreach x in demev demevcf {
	replace `x' = 3 if statea=="DC" & ///
		(year == 1980 | year == 1984 | year == 1992 | ///
			year == 1996 | year == 2004 | year == 2012)
}

*** fix 1988 odd cases ***

foreach x in demev demevcf {
	replace `x' = 36 if statea=="NY" & year == 1988
	replace `x' = 3 if statea=="DC" & year == 1988
	replace `x' = `x' - 1 if statea=="WV" & year == 1988
}

*** fix 2000 odd cases ***

foreach x in demev demevcf {
	replace `x' = 3 if statea=="DC" & year == 2000
	replace `x' = 0 if statea=="FL" & year == 2000
	
}

*** fix 2008 odd cases ***

foreach x in demev demevcf {
	replace `x' = 3 if statea=="DC" & year == 2008 
	replace `x' = + 1 if statea=="NE" & year == 2008 
	
}

*** fix 2016 odd cases ***

replace demev = demev - 1 if year== 2016 & statea=="ME" // ME
replace demevcf = demevcf - 1 if year== 2016 & statea=="ME" // ME
replace demev = demev - 4 if year == 2016 & statea=="WA" // faithless WA
replace demevcf = demevcf - 4 if year == 2016 & statea=="WA" // faithless WA
replace demev = demev - 1 if year == 2016 & statea=="HI" // faithless HI
replace demevcf = demevcf - 1 if year == 2016 & statea=="HI" // faithless HI

foreach x in demev demevcf {
	replace `x' = 3 if statea=="DC" & year == 2016
}

*** aggregate by year ***

collapse (sum) demev demevcf, by(year)

*** one abstention in 2000 ***

foreach x in demev demevcf {
	replace `x' = `x' - 1 if year == 2000
}

*** one faithless elector in 2004 (OH) ***

foreach x in demev demevcf {
	replace `x' = `x' - 1 if year == 2004
}

gen threshold = .
replace threshold = 266 if year == 1952 | year == 1956
replace threshold = 269 if year == 1960
replace threshold = 270 if year > 1960

replace demev = demev - threshold
replace demevcf = demevcf - threshold

gen demevcflab = string(demevcf ) 
replace demevcflab = "+" + demevcflab if demevcf > 0
replace demevcflab = demevcflab + "*" if sign(demevcf)!=sign(demev)

#delimit;

gr tw
	(con demev year, col(gray) msize(tiny))
	(con demevcf year, col(black) msize(tiny) lpat(solid))
	//(line demevcf2 year, col(gray))	
	(sc demevcf year, msym(none) mlab(demevcflab) mlabpos(2) 
		mlabsize(small))
	,
		yline(0, lpat(dash))
		plotregion(style(none))
		legend(off)
		ylab(, angle(horiz))
		xlab(1960(4)2016, angle(45))
		ytitle("Democrat Electoral College margin")
		xtitle("Year")
		text(50 1987 "Actual{&rarr}", col(gray))
		text(-100 1997 "{&larr}Counterfactual", col(black))
		text(-300 2008 "* denotes a pivotal change", size(small))
		;

#delimit cr

gr export "figure 7.pdf", replace
